<?php
/**
 * XueYuan Project
 *
 * @package	XueYuan 
 * @author	Jiuchi Team
 * @since	Version 1.0.0
 * @filesource
 */
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * User controller class
 * @author Wayne Yu
 */
class Home extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->library('session');
		$this->load->library('apioutput');
		$this->load->library('msgblock');
		$this->load->helper('url');
		$uid = $this->session->userdata('uid');
		if(empty($uid)){
			redirect(site_url('login'));
		}
		$this->load->model('user_model');
		$this->load->model('user_concern_model');
		$this->load->model('user_dynamic_model');
		$this->load->model('message_inform_model');
		$this->load->model('message_at_model');
		$this->load->model('message_letter_model');
	}
	
	/**
	 * Personal center dashboard
	 */
	public function dynamic(){
		$userInfo = $this->user_model->getInfo($this->session->userdata('uid'));
		$data = array(
	
		);
		$this->load->view('home',$data);
	}
	
	public function msg(){
		$userInfo = $this->user_model->getInfo($this->session->userdata('uid'));
		$data = array(
			
		);
		$this->load->view('msghome',$data);
	}
	
	public function at(){
		$userInfo = $this->user_model->getInfo($this->session->userdata('uid'));
		$data = array(
					
		);
		$this->load->view('athome',$data);
	}
	
	public function letter(){
		$userInfo = $this->user_model->getInfo($this->session->userdata('uid'));
		$data = array(
					
		);
		$this->load->view('letterhome',$data);
	}
	
	public function getDynamic(){
		$input = $this->input->post();
		$pageSize = isset($input['amount'])?$input['amount']:5;
		$laseItem = isset($input['last'])?$input['last']:0;
		
		$concernedArr = array();
		$data = $this->user_concern_model->getConcerned($this->session->userdata('uid'));
		foreach ($data as $row){
			$concernedArr[] = $row['concern_id'];
		}
		$data = $this->user_dynamic_model->getDynamic($concernedArr, $input['amount'], $input['last']);
		
		$dynamicArr = array();
		foreach ($data as $row){
			$content = '';
			if($row['content_img']){
				if($row['type'] != 3){
					$content .= '<div class="rep-img"><img src="'.$row['content_img'].'" class="rep-img"></div>';
				} else{
					$content .= '<div class="rep-cir-img"><img src="'.$row['content_img'].'" class="img-cir"></div>';
				}
			}
			$content .= '<div class="rep-comment">'.$row['content'].'</div>';
			
			$uInfo  = $this->user_model->getInfo($row['user_id']);
			
			$dynamicArr[] = array(
					'headportrait' =>	'<a href="'.site_url('user/view/'.$row['user_id']).'"><img style="width:44px;" src="'.(isset($uInfo['headportrait'])?$uInfo['headportrait']:'').'" class="img-avatar"></a>',
					'name'			=>	isset($uInfo['nickname'])?'<a href="'.site_url('user/view/'.$row['user_id']).'">'.$uInfo['nickname'].'</a>':'',
					'type'			=>	'发表了话题',
					'title'			=>	$row['title'],
					'content'		=>	$content,
					'from'			=>	$row['circle_name'],
					'date'			=>	$this->user_dynamic_model->getTimeFormat(strtotime($row['created_at']))
			);
		}
		
		echo json_encode($dynamicArr);
	}
	
	public function getMsg(){
		$input = $this->input->post();
		$pageSize = isset($input['amount'])?$input['amount']:5;
		$laseItem = isset($input['last'])?$input['last']:0;
	
		$hintArr = array(
				1	=>	'评论了你的话题',
				2	=>	'回复了你的评论',
				3	=>	'回复了你',
				4	=>	'赞了你的评论'
		);
		$urlArr = array(
				1	=>	'topic/view/',
				2	=>	'comment/view/',
				3	=>	'comment/view/',
				4	=>	'comment/view/'
		);
		
		$data = $this->message_inform_model->getUserInforms($this->session->userdata('uid'), $pageSize, $laseItem);
		foreach ($data as $key => $val){
			$uInfo  = $this->user_model->getInfo($val['informer_id']);
			$data[$key] = array(
					'name'		=>	isset($uInfo['nickname'])?'<a href="'.site_url('user/view/'.$val['user_id']).'">'.$uInfo['nickname'].'</a>':'',
					'hint'		=>	isset($hintArr[$val['inform_type']])?$hintArr[$val['inform_type']]:'',
					'topicname'	=>	'<a href="'.site_url($urlArr[$val['inform_type']].$val['obj_id']).'">'.(isset($val['title'])?$val['title']:'').'</a>'
			);
		}
	
		echo json_encode($data);
	}
	
	public function getAt(){
		$input = $this->input->post();
		$pageSize = isset($input['amount'])?$input['amount']:5;
		$laseItem = isset($input['last'])?$input['last']:0;
	
		$urlArr = array(
				1	=>	'topic/view/',
				2	=>	'comment/view/'
		);
	
		$data = $this->message_at_model->getUserAts($this->session->userdata('uid'), $pageSize, $laseItem);
		foreach ($data as $key => $val){
			$uInfo  = $this->user_model->getInfo($val['ater_id']);
			$data[$key] = array(
					'name'		=>	isset($uInfo['nickname'])?'<a href="'.site_url('user/view/'.$val['user_id']).'">'.$uInfo['nickname'].'</a>':'',
					'link'		=>	'<a href="'.site_url($urlArr[$val['at_type']].$val['obj_id']).'">@了你</a>'
			);
		}
	
		echo json_encode($data);
	}
	
	public function getLetter(){
		$input = $this->input->post();
		$pageSize = isset($input['amount'])?$input['amount']:5;
		$laseItem = isset($input['last'])?$input['last']:0;
	
	
		$data = $this->message_letter_model->getUserLetters($this->session->userdata('uid'), $pageSize, $laseItem);
		foreach ($data as $key => $val){
			$uInfo  = $this->user_model->getInfo($val['sender_id']);
			$data[$key] = array(
					'name'		=>	isset($uInfo['nickname'])?'<a href="'.site_url('user/view/'.$val['user_id']).'">'.$uInfo['nickname'].'</a>':'',
					'link'		=>	'<a href="'.site_url('mycenter/letter/'.$val['id']).'">私信了你</a>'
			);
		}
	
		echo json_encode($data);
	}
}